//
//  PictirePurchaseViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/14.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "PictirePurchaseViewModel.h"
#import "AdressSelectTableViewCell.h"
#import "CardlistTableViewCell.h"
#import "PicturePurTableViewCell.h"

@interface PictirePurchaseViewModel ()<UITableViewDelegate,UITableViewDataSource>
{
    NSString * dayStr;
    NSString * phoneStr;
}
@end

@implementation PictirePurchaseViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(44);
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableHeaderView = [self createHeaderView];
        [superView addSubview:tableView];
        //
        GLGWButton * bottomBut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, kY_With_View(tableView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) hTitle:@"提交求购单" titleLableFont:SYSTEMFONT(16) titleLableColor:[UIColor whiteColor] theCornerRadius:0 backgroundColr:kColor_Rule_Yellow inSuperView:superView];
        [bottomBut addTarget:self action:@selector(picturePurAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
-(UIView *)createHeaderView{
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(10))];
    headerView.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    return headerView;
}

#pragma mark --- set
-(void)setPubData:(PublishListModel_Data *)pubData{
    _pubData = pubData;
}
#pragma mark -- 数据请求
-(void)picturePurAction{
    if (_pubData.productId == 0) {
        [GLGWProgressHUD showError:@"请选择品名！" view:nil];
        return;
    }
    if (dayStr.length == 0) {
        [GLGWProgressHUD showError:@"请填写报价截止天数！" view:nil];
        return;
    }
    if (phoneStr.length == 0) {
        [GLGWProgressHUD showError:@"请填写联系人手机号！" view:nil];
        return;
    }
    NSDictionary * picDic = @{@"userId":koString(self.userId),
                              @"type":@3,
                              @"quoteLimit":@([dayStr integerValue]),
                              @"contacts":@"",
                              @"contactPhone":koString(phoneStr),
                              @"productId":@(_pubData.productId),
                              @"picUrls":@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545213054195&di=323d0cf863e5456700fa8660a784c103&imgtype=0&src=http%3A%2F%2Fpic42.photophoto.cn%2F20170115%2F1155116818621293_b.jpg,https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1545213077355&di=4f0fc29b342c05d163a83be051268412&imgtype=0&src=http%3A%2F%2Fpic164.nipic.com%2Ffile%2F20180514%2F24821412_143226344000_2.jpg",
                              @"titleWord":@""};
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kPubpicProxychases] lodingView:nil lodingTextString:@"" isLoding:YES parameters:picDic success:^(id  _Nullable response) {
        
    } failure:^(NSError * _Nullable error) {
        
    }];
}
#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat cellHeight;
    if (indexPath.row == 1) {
        cellHeight = kHeightProportion_In_iPhoneTriploid(142);
    }else{
        cellHeight = kHeightProportion_In_iPhoneTriploid(44);
    }
    return cellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.row) {
        case 0:
        case 3:{
            AdressSelectTableViewCell * cell_name = [AdressSelectTableViewCell cellWithTableView:tableView];
            NSString * leftStr;
            if (indexPath.row == 0) {
                leftStr = @"品名";
                cell_name.showPush = YES;
                [cell_name.inputTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                    self->dayStr = x;
                }];
            }else{
                leftStr = @"联系人手机号";
                cell_name.inputTextField.userInteractionEnabled = YES;
                cell_name.inputTextField.placeholder = @"请填写联系人手机号";
                cell_name.inputTextField.textAlignment = NSTextAlignmentLeft;
                [cell_name.inputTextField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
                    self->phoneStr = x;
                }];
            }
            cell_name.leftTitString = leftStr;
            cell = cell_name;
        }
            break;
        case 1:{
            PicturePurTableViewCell * cell_pic = [PicturePurTableViewCell cellWithTableView:tableView];
            cell = cell_pic;
        }
            break;
        case 2:{
            CardlistTableViewCell * cell_Card = [CardlistTableViewCell cellWithTableView:tableView];
            cell_Card.c_type = CellType_Picture;
            cell_Card.nameLeftStr = @"报名截止时间";
            cell_Card.unitStr = @"天";
            cell_Card.changeTextField.userInteractionEnabled = YES;
            cell_Card.changeTextField.placeholder = @"请填写天数";
            cell = cell_Card;
        }
            break;
            
        default:
            break;
    }
    if (cell == nil) {
        cell = [BaseTableViewCell cellWithTableView:tableView];
    }
    [cell setSepartionLineWith:lineModel_Half];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(selectProductName)]) {
            [self.delegate selectProductName];
        }
    }
}
@end
